﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ComTools
{
	public partial class frmMain : Form
	{
		public frmMain()
		{
			InitializeComponent();
		}

		internal bool IsMultitance { get; set; }

		/// <summary>
		/// 加载指定类型的窗体
		/// </summary>
		/// <param name="formtype">窗体类型</param>
		private void OpenForm(Type formtype, bool multiInstance = false)
		{
			Form form = null;
			if (!multiInstance)
			{
				form = this.MdiChildren.FirstOrDefault(n => n.GetType() == formtype);
			}
			if (form == null)
			{
				object formobj = formtype.Assembly.CreateInstance(formtype.FullName);
				form = formobj as Form;
				OpenForm(form);
			}
			else
				form.Activate();
		}

		private void OpenForm(Form form)
		{
			if (this.MdiChildren.Length == 0)
			{
				form.WindowState = FormWindowState.Normal;
			}
			else
			{
				form.WindowState = this.MdiChildren[0].WindowState;
			}
			form.MdiParent = this;
			//form.ShowIcon = false;
			form.ShowInTaskbar = false;
			form.Show();
		}

		private void OpenForm(string package, string formName)
		{
			try
			{
				string filePath = Path.Combine(Application.StartupPath, package);

				Assembly assembly = Assembly.LoadFile(filePath);
				object obj = assembly.CreateInstance(formName);
				Form f = obj as Form;
				OpenForm(f);
			}
			catch (FileNotFoundException fnfe)
			{
				//找不到要加载的文件
				throw fnfe;
			}
			catch (FileLoadException fle)
			{
				//无法加载的文件
				throw fle;
			}
			catch (BadImageFormatException bife)
			{
				//不是有效的File
				throw bife;
			}
		}

		private void MAIN0009_Click(object sender, EventArgs e)
		{
			Application.Exit();
		}

		private void MAIN0101_Click(object sender, EventArgs e)
		{
			OpenForm(typeof(frmSqlServer));
		}

		private void MAIN0102_Click(object sender, EventArgs e)
		{
			OpenForm(typeof(frmMySqlDBManager), IsMultitance);
		}

		private void MAIN0103_Click(object sender, EventArgs e)
		{
			
		}

		private void MAIN0104_Click(object sender, EventArgs e)
		{
			
		}

		private void MAIN0105_Click(object sender, EventArgs e)
		{
			
		}

		private void MAIN0106_Click(object sender, EventArgs e)
		{
			
		}

		private void MAIN0107_Click(object sender, EventArgs e)
		{
			
		}

		private void MAIN0108_Click(object sender, EventArgs e)
		{
			OpenForm(typeof(frmCheckData));
		}


		private void MAIN0201_Click(object sender, EventArgs e)
		{
			OpenForm(typeof(Form1), IsMultitance);
		}

		private void MAIN0202_Click(object sender, EventArgs e)
		{
			OpenForm(typeof(frmMQTT), IsMultitance);
		}

		private void MAIN0203_Click(object sender, EventArgs e)
		{
			OpenForm(typeof(frmSendPost), IsMultitance);
		}

		private void MAIN0207_Click(object sender, EventArgs e)
		{
			OpenForm(typeof(frmEncrypt));
		}

		private void MAIN0209_Click(object sender, EventArgs e)
		{
			OpenForm(typeof(frmMailSend));
		}

		private void MAIN0212_Click(object sender, EventArgs e)
		{
			OpenForm(typeof(frmTools), IsMultitance);
		}

		private void FrmMain_Load(object sender, EventArgs e)
		{

		}

        private void MAIN0214_Click(object sender, EventArgs e)
        {
			OpenForm(typeof(frmLG220L), IsMultitance);
        }

        private void MAIN0215_Click(object sender, EventArgs e)
        {
			OpenForm(typeof(frmCamera), IsMultitance);
        }

		private void MAIN0216_Click(object sender, EventArgs e)
		{
			OpenForm(typeof(frmSqlDependency));
		}

		private void MAIN0305_Click(object sender, EventArgs e)
		{
			OpenForm(typeof(Form2), true);
		}

		private void MAIN0306_Click(object sender, EventArgs e)
		{
			OpenForm(typeof(frmSocketServer), true);
		}

		private void MAIN0307_Click(object sender, EventArgs e)
		{
			OpenForm(typeof(frmSocketClient), true);
		}

		private void MAIN0308_Click(object sender, EventArgs e)
		{
			OpenForm(typeof(frmLG220L2), true);
		}

		private void MAIN0309_Click(object sender, EventArgs e)
        {
			OpenForm(typeof(frmWebApi));
		}

        private void MAIN0401_Click(object sender, EventArgs e)
        {
			OpenForm(typeof(frmMsSqlSet));
        }


        private void MAIN0402_Click(object sender, EventArgs e)
        {
			OpenForm(typeof(frmTableEntity));
        }

        private void MAIN0403_Click(object sender, EventArgs e)
        {
			OpenForm(typeof(frm0403));
        }

        private void tsmiFanWei_Click(object sender, EventArgs e)
        {
			OpenForm(typeof(frmFanWei));
        }

        private void tsmiApiCall_Click(object sender, EventArgs e)
        {
			OpenForm(typeof(frmApiCall));
		}
    }
}